package com.example.fengyungo.controller.admin;

import com.example.fengyungo.constant.MsgConstant;
import com.example.fengyungo.domain.pojo.DriverVerify;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.DriverVerifyService;
import com.example.fengyungo.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "风运Go管理端-司机认证信息管理")
@RestController
@RequestMapping("/admin/driverVerify")
public class AdminDriverVerifyController {

    @Autowired
    private DriverVerifyService driverVerifyService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询所有司机认证信息
     *
     * @return Result
     */
    @ApiOperation("查询所有司机认证信息")
    @GetMapping("/getAllDriverVerify")
    @Cacheable(value = "driverVerify", key = "'getAllDriverVerify'")
    public Result<List<DriverVerify>> getAllDriverVerify() {
        List<DriverVerify> allDriverVerify = driverVerifyService.getAllDriverVerify();
        return Result.success(allDriverVerify);
    }

    /**
     * 查询所有司机认证信息
     *
     * @return Result
     */
    @ApiOperation("查询未处理的司机认证信息")
    @GetMapping("/driverVerifyNotHandled")
    @Cacheable(value = "driverVerify", key = "'driverVerifyNotHandled'")
    public Result<List<DriverVerify>> getAllDriverVerifyNotHandled() {
        List<DriverVerify> allDriverVerify = driverVerifyService.getAllDriverVerifyNotHandled();
        return Result.success(allDriverVerify);
    }

    /**
     * 修改司机认证信息状态（是否已处理）
     *
     * @param id     String
     * @param status Integer
     * @return Result
     */
    @ApiOperation("修改司机认证信息状态（是否已处理）")
    @PutMapping("/updateDriverVerifyStatus/{id}/{status}")
    public Result updateDriverVerifyStatus(@PathVariable String id, @PathVariable Integer status) {
        Integer res = driverVerifyService.updateDriverVerifyStatusById(id, status);
        if (res > 0) {
            // 清除缓存
            redisUtil.cleanCache("fengyungodriverVerify*");
            return Result.success();
        } else {
            return Result.error(MsgConstant.UPDATE_FAIL);
        }
    }
}
